<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>使用一个字符串分割另一个字符串</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.explode" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">explode</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">explode</span> &mdash; <span class="dc-title">使用一个字符串分割另一个字符串</span></p>
 
 </div>
 
 <div class="refsect1 description" id="refsect1-function.explode-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>explode</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$delimiter</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$string</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$limit</code></span>
  ] ) : <span class="type">array</span></div>

  <p class="para rdfs-comment">
   此函数返回由字符串组成的数组，每个元素都是
   <code class="parameter">string</code>
   的一个子串，它们被字符串
   <code class="parameter">delimiter</code>
   作为边界点分割出来。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.explode-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">delimiter</code></dt>

     <dd>

      <p class="para">
       边界上的分隔字符。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">string</code></dt>

     <dd>

      <p class="para">
       输入的字符串。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">limit</code></dt>

     <dd>

      <p class="para">
       如果设置了
   <code class="parameter">limit</code>
   参数并且是正数，则返回的数组包含最多
   <code class="parameter">limit</code>
   个元素，而最后那个元素将包含
   <code class="parameter">string</code>
   的剩余部分。
      </p>
      <p class="para">
       如果
  <code class="parameter">limit</code>
  参数是负数，则返回除了最后的
  -<code class="parameter">limit</code>
  个元素外的所有元素。
      </p>
      <p class="para">
       如果 <code class="parameter">limit</code> 是 0，则会被当做 1。
      </p>
     </dd>

    
   </dl>

  </p>
  <p class="para">
   由于历史原因，虽然 <span class="function"><a href="implode.html" class="function">implode()</a></span>
  可以接收两种参数顺序，但是
  <span class="function"><strong>explode()</strong></span> 不行。你必须保证
  <code class="parameter">separator</code> 参数在
  <code class="parameter">string</code> 参数之前才行。
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.explode-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   此函数返回由字符串组成的 <span class="type"><a href="language.types.array.html" class="type array">array</a></span>，每个元素都是
   <code class="parameter">string</code>
   的一个子串，它们被字符串
   <code class="parameter">delimiter</code>
   作为边界点分割出来。
  </p>
  <p class="para">
   如果 <code class="parameter">delimiter</code>
   为空字符串（&quot;&quot;），<span class="function"><strong>explode()</strong></span>
   将返回 <strong><code>FALSE</code></strong>。
   如果
   <code class="parameter">delimiter</code>
   所包含的值在
   <code class="parameter">string</code>
   中找不到，并且使用了负数的 <code class="parameter">limit</code> ，
   那么会返回空的 <span class="type"><a href="language.types.array.html" class="type array">array</a></span>，
   否则返回包含 <code class="parameter">string</code>
   单个元素的数组。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.explode-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.1.0</td>
       <td>
        支持负数的  <code class="parameter">limit</code>
       </td>
      </tr>

      <tr>
       <td>4.0.1</td>
       <td>
          增加了参数 <code class="parameter">limit</code>
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.explode-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-5903">
    <p><strong>Example #1 <span class="function"><strong>explode()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;示例&nbsp;1<br /></span><span style="color: #0000BB">$pizza&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"piece1&nbsp;piece2&nbsp;piece3&nbsp;piece4&nbsp;piece5&nbsp;piece6"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$pieces&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">explode</span><span style="color: #007700">(</span><span style="color: #DD0000">"&nbsp;"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$pizza</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">$pieces</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">];&nbsp;</span><span style="color: #FF8000">//&nbsp;piece1<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$pieces</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">];&nbsp;</span><span style="color: #FF8000">//&nbsp;piece2<br /><br />//&nbsp;示例&nbsp;2<br /></span><span style="color: #0000BB">$data&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"foo:*:1023:1000::/home/foo:/bin/sh"</span><span style="color: #007700">;<br />list(</span><span style="color: #0000BB">$user</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$pass</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$uid</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$gid</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$gecos</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$home</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$shell</span><span style="color: #007700">)&nbsp;=&nbsp;</span><span style="color: #0000BB">explode</span><span style="color: #007700">(</span><span style="color: #DD0000">":"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$data</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">$user</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;foo<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$pass</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;*<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
    <p class="para">
   <div class="example" id="example-5904">
    <p><strong>Example #2 <span class="function"><strong>explode()</strong></span> return examples</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/*&nbsp;A&nbsp;string&nbsp;that&nbsp;doesn't&nbsp;contain&nbsp;the&nbsp;delimiter&nbsp;will&nbsp;simply&nbsp;return&nbsp;a&nbsp;one-length&nbsp;array&nbsp;of&nbsp;the&nbsp;original&nbsp;string.&nbsp;*/<br /></span><span style="color: #0000BB">$input1&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"hello"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$input2&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"hello,there"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">explode</span><span style="color: #007700">(&nbsp;</span><span style="color: #DD0000">','</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$input1&nbsp;</span><span style="color: #007700">)&nbsp;);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">explode</span><span style="color: #007700">(&nbsp;</span><span style="color: #DD0000">','</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$input2&nbsp;</span><span style="color: #007700">)&nbsp;);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
array(1)
(
    [0] =&gt; string(5) &quot;hello&quot;
)
array(2)
(
    [0] =&gt; string(5) &quot;hello&quot;
    [1] =&gt; string(5) &quot;there&quot;
)
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-5905">
    <p><strong>Example #3 <code class="parameter">limit</code> 参数的例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$str&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'one|two|three|four'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;正数的&nbsp;limit<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">explode</span><span style="color: #007700">(</span><span style="color: #DD0000">'|'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$str</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">//&nbsp;负数的&nbsp;limit（自&nbsp;PHP&nbsp;5.1&nbsp;起）<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">explode</span><span style="color: #007700">(</span><span style="color: #DD0000">'|'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$str</span><span style="color: #007700">,&nbsp;-</span><span style="color: #0000BB">1</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [0] =&gt; one
    [1] =&gt; two|three|four
)
Array
(
    [0] =&gt; one
    [1] =&gt; two
    [2] =&gt; three
)
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.explode-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">此函数可安全用于二进制对象。</span></p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.explode-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="preg_split.html" class="function" rel="rdfs-seeAlso">preg_split()</a> - 通过一个正则表达式分隔字符串</span></li>
    <li class="member"><span class="function"><a href="str_split.html" class="function" rel="rdfs-seeAlso">str_split()</a> - 将字符串转换为数组</span></li>
    <li class="member"><span class="function"><a href="mb_split.html" class="function" rel="rdfs-seeAlso">mb_split()</a> - 使用正则表达式分割多字节字符串</span></li>
    <li class="member"><span class="function"><a href="str_word_count.html" class="function" rel="rdfs-seeAlso">str_word_count()</a> - 返回字符串中单词的使用情况</span></li>
    <li class="member"><span class="function"><a href="strtok.html" class="function" rel="rdfs-seeAlso">strtok()</a> - 标记分割字符串</span></li>
    <li class="member"><span class="function"><a href="implode.html" class="function" rel="rdfs-seeAlso">implode()</a> - 将一个一维数组的值转化为字符串</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>